home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / daemons / lpd / RCS / common.c,v < prev    next >
Encoding:
Text File  |  1990-02-16  |  6.8 KB  |  303 lines

  1. head     1.2;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.2
  10. date     90.02.16.14.11.57;  author rab;  state Exp;
  11. branches ;
  12. next     1.1;
  13.  
  14. 1.1
  15. date     88.11.23.10.33.54;  author rab;  state Exp;
  16. branches ;
  17. next     ;
  18.  
  19.  
  20. desc
  21. @@
  22.  
  23.  
  24. 1.2
  25. log
  26. @Added some extra debugging info when trying to talk to remote hosts.
  27. @
  28. text
  29. @/*
  30.  * Copyright (c) 1983 Regents of the University of California.
  31.  * All rights reserved.
  32.  *
  33.  * Redistribution and use in source and binary forms are permitted
  34.  * provided that this notice is preserved and that due credit is given
  35.  * to the University of California at Berkeley. The name of the University
  36.  * may not be used to endorse or promote products derived from this
  37.  * software without specific prior written permission. This software
  38.  * is provided ``as is'' without express or implied warranty.
  39.  */
  40.  
  41. #ifndef lint
  42. static char sccsid[] = "@@(#)common.c    5.3 (Berkeley) 5/5/88";
  43. #endif /* not lint */
  44.  
  45. /*
  46.  * Routines and data common to all the line printer functions.
  47.  */
  48.  
  49. #include "lp.h"
  50.  
  51. int    DU;        /* daeomon user-id */
  52. int    MX;        /* maximum number of blocks to copy */
  53. int    MC;        /* maximum number of copies allowed */
  54. char    *LP;        /* line printer device name */
  55. char    *RM;        /* remote machine name */
  56. char    *RP;        /* remote printer name */
  57. char    *LO;        /* lock file name */
  58. char    *ST;        /* status file name */
  59. char    *SD;        /* spool directory */
  60. char    *AF;        /* accounting file */
  61. char    *LF;        /* log file for error messages */
  62. char    *OF;        /* name of output filter (created once) */
  63. char    *IF;        /* name of input filter (created per job) */
  64. char    *RF;        /* name of fortran text filter (per job) */
  65. char    *TF;        /* name of troff filter (per job) */
  66. char    *NF;        /* name of ditroff filter (per job) */
  67. char    *DF;        /* name of tex filter (per job) */
  68. char    *GF;        /* name of graph(1G) filter (per job) */
  69. char    *VF;        /* name of vplot filter (per job) */
  70. char    *CF;        /* name of cifplot filter (per job) */
  71. char    *PF;        /* name of vrast filter (per job) */
  72. char    *FF;        /* form feed string */
  73. char    *TR;        /* trailer string to be output when Q empties */
  74. short    SC;        /* suppress multiple copies */
  75. short    SF;        /* suppress FF on each print job */
  76. short    SH;        /* suppress header page */
  77. short    SB;        /* short banner instead of normal header */
  78. short    HL;        /* print header last */
  79. short    RW;        /* open LP for reading and writing */
  80. short    PW;        /* page width */
  81. short    PL;        /* page length */
  82. short    PX;        /* page width in pixels */
  83. short    PY;        /* page length in pixels */
  84. short    BR;        /* baud rate if lp is a tty */
  85. int    FC;        /* flags to clear if lp is a tty */
  86. int    FS;        /* flags to set if lp is a tty */
  87. int    XC;        /* flags to clear for local mode */
  88. int    XS;        /* flags to set for local mode */
  89. short    RS;        /* restricted to those with local accounts */
  90.  
  91. char    line[BUFSIZ];
  92. char    pbuf[BUFSIZ/2];    /* buffer for printcap strings */
  93. char    *bp = pbuf;    /* pointer into pbuf for pgetent() */
  94. char    *name;        /* program name */
  95. char    *printer;    /* printer name */
  96. char    host[32];    /* host machine name */
  97. char    *from = host;    /* client's machine name */
  98.  
  99. static int compar();
  100.  
  101. /*
  102.  * Create a connection to the remote printer server.
  103.  * Most of this code comes from rcmd.c.
  104.  */
  105. getport(rhost)
  106.     char *rhost;
  107. {
  108.     struct hostent *hp;
  109.     struct servent *sp;
  110.     struct sockaddr_in sin;
  111.     int s, timo = 1, lport = IPPORT_RESERVED - 1;
  112.     int err;
  113.     extern int debug;
  114.  
  115.     /*
  116.      * Get the host address and port number to connect to.
  117.      */
  118.     if (rhost == NULL)
  119.         fatal("no remote host to connect to");
  120.     hp = gethostbyname(rhost);
  121.     if (hp == NULL)
  122.         fatal("unknown host %s", rhost);
  123.     sp = getservbyname("printer", "tcp");
  124.     if (sp == NULL)
  125.         fatal("printer/tcp: unknown service");
  126.     bzero((char *)&sin, sizeof(sin));
  127.     bcopy(hp->h_addr, (caddr_t)&sin.sin_addr, hp->h_length);
  128.     sin.sin_family = hp->h_addrtype;
  129.     sin.sin_port = sp->s_port;
  130.  
  131.     /*
  132.      * Try connecting to the server.
  133.      */
  134. retry:
  135.     s = rresvport(&lport);
  136.     if (s < 0) {
  137.             if (debug) {
  138.             syslog("rresvport: %s\n", strerror(errno));
  139.         }
  140.         return(-1);
  141.     }
  142.     if (connect(s, (caddr_t)&sin, sizeof(sin), 0) < 0) {
  143.         err = errno;
  144.         (void) close(s);
  145.         errno = err;
  146.         if (errno == EADDRINUSE) {
  147.             lport--;
  148.             goto retry;
  149.         }
  150.         if (errno == ECONNREFUSED && timo <= 16) {
  151.             sleep(timo);
  152.             timo *= 2;
  153.             goto retry;
  154.         }
  155.             if (debug) {
  156.             syslog("connect: %s\n", strerror(errno));
  157.         }
  158.         return(-1);
  159.     }
  160.     return(s);
  161. }
  162.  
  163. /*
  164.  * Getline reads a line from the control file cfp, removes tabs, converts
  165.  *  new-line to null and leaves it in line.
  166.  * Returns 0 at EOF or the number of characters read.
  167.  */
  168. getline(cfp)
  169.     FILE *cfp;
  170. {
  171.     register int linel = 0;
  172.     register char *lp = line;
  173.     register c;
  174.  
  175.     while ((c = getc(cfp)) != '\n') {
  176.         if (c == EOF)
  177.             return(0);
  178.         if (c == '\t') {
  179.             do {
  180.                 *lp++ = ' ';
  181.                 linel++;
  182.             } while ((linel & 07) != 0);
  183.             continue;
  184.         }
  185.         *lp++ = c;
  186.         linel++;
  187.     }
  188.     *lp++ = '\0';
  189.     return(linel);
  190. }
  191.  
  192. /*
  193.  * Scan the current directory and make a list of daemon files sorted by
  194.  * creation time.
  195.  * Return the number of entries and a pointer to the list.
  196.  */
  197. getq(namelist)
  198.     struct queue *(*namelist[]);
  199. {
  200.     register struct direct *d;
  201.     register struct queue *q, **queue;
  202.     register int nitems;
  203.     struct stat stbuf;
  204.     int arraysz;
  205.     DIR *dirp;
  206.  
  207.     if ((dirp = opendir(SD)) == NULL)
  208.         return(-1);
  209.     if (fstat(dirp->dd_fd, &stbuf) < 0)
  210.         goto errdone;
  211.  
  212.     /*
  213.      * Estimate the array size by taking the size of the directory file
  214.      * and dividing it by a multiple of the minimum size entry. 
  215.      */
  216.     arraysz = (stbuf.st_size / 24);
  217.     queue = (struct queue **)malloc(arraysz * sizeof(struct queue *));
  218.     if (queue == NULL)
  219.         goto errdone;
  220.  
  221.     nitems = 0;
  222.     while ((d = readdir(dirp)) != NULL) {
  223.         if (d->d_name[0] != 'c' || d->d_name[1] != 'f')
  224.             continue;    /* daemon control files only */
  225.         if (stat(d->d_name, &stbuf) < 0)
  226.             continue;    /* Doesn't exist */
  227.         q = (struct queue *)malloc(sizeof(time_t)+strlen(d->d_name)+1);
  228.         if (q == NULL)
  229.             goto errdone;
  230.         q->q_time = stbuf.st_mtime;
  231.         strcpy(q->q_name, d->d_name);
  232.         /*
  233.          * Check to make sure the array has space left and
  234.          * realloc the maximum size.
  235.          */
  236.         if (++nitems > arraysz) {
  237.             queue = (struct queue **)realloc((char *)queue,
  238.                 (stbuf.st_size/12) * sizeof(struct queue *));
  239.             if (queue == NULL)
  240.                 goto errdone;
  241.         }
  242.         queue[nitems-1] = q;
  243.     }
  244.     closedir(dirp);
  245.     if (nitems)
  246.         qsort(queue, nitems, sizeof(struct queue *), compar);
  247.     *namelist = queue;
  248.     return(nitems);
  249.  
  250. errdone:
  251.     closedir(dirp);
  252.     return(-1);
  253. }
  254.  
  255. /*
  256.  * Compare modification times.
  257.  */
  258. static int
  259. compar(p1, p2)
  260.     register struct queue **p1, **p2;
  261. {
  262.     if ((*p1)->q_time < (*p2)->q_time)
  263.         return(-1);
  264.     if ((*p1)->q_time > (*p2)->q_time)
  265.         return(1);
  266.     return(0);
  267. }
  268.  
  269. /*VARARGS1*/
  270. fatal(msg, a1, a2, a3)
  271.     char *msg;
  272. {
  273.     if (from != host)
  274.         printf("%s: ", host);
  275.     printf("%s: ", name);
  276.     if (printer)
  277.         printf("%s: ", printer);
  278.     printf(msg, a1, a2, a3);
  279.     putchar('\n');
  280.     exit(1);
  281. }
  282. @
  283.  
  284.  
  285. 1.1
  286. log
  287. @Initial revision
  288. @
  289. text
  290. @d71 1
  291. a71 1
  292. int compar();
  293. d85 1
  294. d108 4
  295. a111 1
  296.     if (s < 0)
  297. d113 1
  298. d127 3
  299. d230 1
  300. a230 1
  301. static
  302. @
  303.